package com.syq.face;

import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.google.gson.Gson;
import com.syq.face.model.BaseResponseBody;
import com.syq.face.model.JsonResult;
import com.syq.face.model.LoginInfo;
import com.syq.face.model.UserInfo;
import com.syq.face.utils.MyUtils;
import com.syq.face.utils.RetrofitUtil;

import me.yokeyword.fragmentation.SupportActivity;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;

public class StartActivity extends SupportActivity {

    private Button loginBtn;
    private EditText loginName;
    private EditText loginPassword;


    public static final String TAG = "StartActivity";

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.my_login_act);
        init();


    }

    private void init() {
        initView();
        onclick();
    }

    private void initView() {
        loginBtn = findViewById(R.id.login_btn);
        loginName = findViewById(R.id.login_uname);
        loginPassword = findViewById(R.id.login_pass);

        // view-binding
        // butter knife GG - viewBinding
        // Kotlin - viewBinding
    }

    private void onclick() {
        loginBtn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                handleLogin();
            }
        });

    }


    private void handleLogin() {

        String username = loginName.getText().toString();
        String password = loginPassword.getText().toString();
        //验证数据
        RetrofitUtil.getInstance().getUserService().login(username, password)
                .enqueue(new Callback<JsonResult<LoginInfo>>() {
                    @Override
                    public void onResponse(Call<JsonResult<LoginInfo>> call, Response<JsonResult<LoginInfo>> response) {
                        //成功返回
                        JsonResult<LoginInfo> body = response.body();

                        LoginInfo loginInfo = body.getData();
                        Log.d(TAG, "onResponse: " + new Gson().toJson(body));
                        Log.d(TAG, "userinfo:" + loginInfo);

                        if (loginInfo == null) {
                            Toast.makeText(getApplicationContext(), "账号或密码不存在", Toast.LENGTH_SHORT).show();
                            loginName.setText("");
                            loginPassword.setText("");
                        } else {
                            //拿到数据的处理
                            //携带数据跳转
                            Intent intent = new Intent(StartActivity.this, MainActivity.class);
                            MyApplication.setMyself(loginInfo);
                            startActivity(intent);
                            finish();
                        }

                    }

                    @Override
                    public void onFailure(Call<JsonResult<LoginInfo>> call, Throwable t) {
                        Toast.makeText(getApplicationContext(), "网络连接失败", Toast.LENGTH_SHORT).show();
                    }


                });

    }
}

